package com.repair.project.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.repair.project.config.R;
import com.repair.project.entity.Cycle;
import com.repair.project.entity.Material;
import com.repair.project.service.MaterialService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hz
 * @since 2021-01-12
 */
@RestController
@RequestMapping("/project/material")
public class MaterialController {



    @Autowired
    MaterialService materialService;

    @ApiOperation("接口描述：获取所有物料列表")
    @GetMapping("getMatList")
    public R getMatList(@RequestParam(value = "page")int page, @RequestParam(value = "limit")int limit,
                        @RequestParam(required = false, value = "materialid")Integer materialid,
                        @RequestParam(required = false, value = "name")String name ){

        List<Material> mats =materialService.list(null);

        Page<Material> matPage = new Page<>(page, limit);

        QueryWrapper<Material> objectQueryWrapper = new QueryWrapper<>();
        if(materialid!=null) {
            objectQueryWrapper.eq("materialid", materialid);
        }
        if(name!=null) {
            objectQueryWrapper.like("name", name);
        }
        IPage<Material> matIPage = materialService.page(matPage, objectQueryWrapper);
        return R.ok().data("datalist",matIPage);
    }

    @ApiOperation("接口描述:增加物料")
    @PostMapping("addMat")
    public R addCycle(@RequestBody Material material){
        return R.ok().data("data",materialService.save(material));
    }

    @ApiOperation("接口描述：删除物料")
    @GetMapping("delMat")
    public R delCycle(@RequestParam(value = "materialid")int materialid){
        return R.ok().data("data",materialService.removeById(materialid));
    }

    @ApiOperation("接口描述：更新物料")
    @PostMapping("updMat")
    public R updCycle(@RequestBody Material material){
        return R.ok().data("data",materialService.updateById(material));
    }


}

